
package com.scalar.chiptrack.sales.actions;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.scalar.ScalarException;
import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.Labels;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.sales.dao.CustomerDAO;
import com.scalar.chiptrack.sales.forms.AddShippingMethodForm;
import com.scalar.chiptrack.users.User;


public class AddShippingMethodAction extends ChipTrackAction {

    private static final String ACTION_NAME = "/addShippingMethod.do" ;  // Added by Basha (08/08/2003)

    private ArrayList allshippingmethods = new ArrayList();
    private String shipmethodvalue = null;
    private String account = null;
    private boolean checkmethodexist = true;

    public ActionForward execute (ActionMapping mapping,
                                      ActionForm form,
                                      HttpServletRequest request,
                                      HttpServletResponse response)
                                     throws Exception {

		HashMap errors = new HashMap();
		request.setAttribute (SessionKeys.ERROR_MESSAGES, errors);

		HashMap info = new HashMap();
		request.setAttribute (SessionKeys.INFO_MESSAGES, info);


		AddShippingMethodForm addshippingmethodform = (AddShippingMethodForm) form;
		String action = request.getParameter("action");
		String customerId = request.getParameter ("customerId");
		//System.out.println ("customer id =" + customerId);

		int customerIdvalue = -1;

		HttpSession session = request.getSession();
        User user = (User)session.getAttribute (SessionKeys.USER_KEY);
        // Added by Basha (08/08/2003)
             if (user == null)
             {
                session.setAttribute (SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString());
                return (mapping.findForward ("login"));
             }
        // Added by Basha (08/08/2003)

		if (customerId != null) {
			session.setAttribute ("addShippingMethodCustomerId", customerId);
		  }
		else {
			customerId = (String) session.getAttribute ("addShippingMethodCustomerId");

		    }

				if("insertShippingMethod".equalsIgnoreCase(action))
				{
                    boolean success = false;
					boolean temp = false;

                    shipmethodvalue = addshippingmethodform.getShippingmethod();
     			 	account = addshippingmethodform.getAccount();

		     		//info.put ("Values of Shipping Method    "+shipmethodvalue+"  and Account  "+ account, null);
					customerIdvalue = Integer.parseInt(customerId);
			 	    checkmethodexist = CustomerDAO.doesShippingMethodExist (null, customerIdvalue, shipmethodvalue, account);

				  if(checkmethodexist == false)
            	    {
                       //These Labels are added by Kumaraswamy on Oct 8th, 2004.
                        String newShippingMethodLabel = null;
                        String shippingMethodLabel = null;
                        try
                        {
                            newShippingMethodLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.salesOrder.NewShippingMethod" );
                            shippingMethodLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.salesOrder.ShippingMethod" );
                            if ( newShippingMethodLabel == null )
                            {
                                newShippingMethodLabel = Labels.DEFAULT_NEW_SHIPPING_METHOD_LABEL;
                            }
                            if ( shippingMethodLabel == null )
                            {
                                shippingMethodLabel = Labels.DEFAULT_SHIPPING_METHOD_LABEL;
                            }
                        }
                        catch ( NullPointerException e )
                        {
                             newShippingMethodLabel = Labels.DEFAULT_NEW_SHIPPING_METHOD_LABEL;
                             shippingMethodLabel    = Labels.DEFAULT_SHIPPING_METHOD_LABEL;
                        }
						try {
							CustomerDAO.insertShippingMethod (null,customerIdvalue,shipmethodvalue,account);
							info.put ( newShippingMethodLabel+" has been stored in database successfully", null );
                            //info.put ( INFO_SHIPPING_METHOD_SUCCESSFULLY_STORED, null );
							success = true;
						}
						catch (ScalarException se) {
							errors.put ("Internal Server Error", "Could not store "+shippingMethodLabel+" Value" +shipmethodvalue);
                            errors.put( ERROR_MESSAGE_CONTACT_SYS_ADMN, null );
                            //session.setAttribute( SessionKeys.ERROR_REPORT_SUBJECT, "Error: Could not load Commercial Invoice for Customer Shipping ID: " + customerShippingId );
                            session.setAttribute( SessionKeys.ERROR_REPORT, se );
							//se.printStackTrace();
						}
						return (mapping.findForward ("success"));
			        }

			        else
			        {   errors.put (""+shipmethodvalue ,null);

	                 }

			}//if


	      return (mapping.findForward ("success"));


	   }//execute

   }
